package com.batchOperation.excel.OperationRecord.serviceImpl;

import com.batchOperation.Template.model.BatchOperationTemplateContent;
import com.batchOperation.excel.OperationRecord.OperationRecord;
import com.batchOperation.excel.OperationRecord.model.Record;
import com.batchOperation.excel.model.systemclass.RowId;
import com.batchOperation.tool.Response;
import com.google.gson.Gson;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @Package: com.batchOperation.excel.OperationRecord.serviceImpl
 * @ClassName: OperationRecordImpl
 * @Author: wulongkai
 * @CreateTime: 2024/7/19 14:31
 * @Description:
 */
@Service
public class OperationRecordImpl implements OperationRecord {

    private Map<Long, List<Record>> db = new ConcurrentHashMap<>();

    @Override
    public void save(Long executorId, Integer rowId, String entity, String reason) {
        Record record = new Record();
        record.setRowId(rowId);
        record.setEntity(entity);
        record.setReason(reason);

        List<Record> contentOperationRecords = db.computeIfAbsent(executorId, K -> new ArrayList<>());
        contentOperationRecords.add(record);
    }

    @Override
    public Response<Map<String, List<Record>>> report(Long executorId) {
        Response<Map<String, List<Record>>> result = Response.getSuccessResult();
        List<Record> records = db.get(executorId);
        if (records != null && !records.isEmpty()) {
            Map<String, List<Record>> stringListMap = records.stream().collect(Collectors.groupingBy(
                    Record::getEntity,
                    Collectors.toList()
            ));
            result.setData(stringListMap);
        }
        return result;
    }
}
